<?php

class PhpFox_Mod_Video_ComponentEdit extends BaseComponent
{
    function process()
    {
    	App::checkUserAccess('video', array('full'));

    	$oSrvVideo = &$this->_oModule->getService('Video');
    	$oReq = &App::getRequest();
    	$oUrl = &App::getUrl();

		if ( ($iVidDelete = $oReq->getInt('dvideo')) )
		{
			$bIsDeleted = $oSrvVideo->deleteVideo($iVidDelete, true);
			$oSrvAccount = &App::getModuleService('Account', 'Account');
			$oSecurityService = &App::getModuleService('Account', 'Security');
			$oSrvAccount->changeUserInfo($oSecurityService->getCurrentUser(), 'video', 0);
			App::gotoUrl(App::getPageName(), array('page' => $oReq->get('page')), App::format('Mod_Video.video_deleted'), false);
		}

    	App::loadClass('Pager');

		$iPageSize = 10;

        if (!$iPageSize)
        {
            $iPage = 0;
            $oUrl->clearParams('page');
        }
        else
        {
            $iPage = ($this->getParam('page') ? $this->getParam('page')  : $oReq->getInt('page', 1));
        }

    	$iCnt = $this->oDatabase->getField("SELECT COUNT(*) AS count FROM ". App::getT('videos') ." AS v
        	WHERE v.vid_userid = '". App::getUser('id') ."'");

		$iOffset = $this->oDatabase->_getPageNum($iPage, $iPageSize, $iCnt);

   		$aItems = $this->oDatabase->getRows("SELECT v.vid_id, v.vid_list_id, v.vid_title, v.vid_url, v.stream_id, v.vid_time, v.vid_total, v.vid_rating, v.vid_info, v.vid_tags, v.vid_rating_count, v.featured, v.duration FROM
   			". App::getT('videos') ." AS v
   			WHERE v.vid_userid = '". App::getUser('id') ."'
   			ORDER BY v.vid_time DESC LIMIT ". $iOffset .",". $iPageSize ."");

		$oSrvVideo = &App::getModuleService('Video', 'Video');
		$aContent = array();
		foreach($aItems as $aItem)
		{
			$aItem['stars'] = $oSrvVideo->prepareStars($aItem['vid_rating']);
			$aContent[] = $aItem;
		}

		$oPager = &new Pager($iCnt, $iPage, $iPageSize);
		$iFrom = $oPager->getOffset($iPage, $iPageSize);
		$iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

		$this->oTpl->assign(array
		(
			'aCategories' => $oSrvVideo->getCategories(),
			'sGImageUrl' => App::getParam('video.image.url'),
			'aItems' => $aContent,
        	'iCnt'             => $iCnt,
        	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
			'aPaging'          => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        	'iTo'              => $iTo,
        	'bIsDeleted' => (isset($bIsDeleted) ? true : false),
        	'aStreamOptions' => App::getParam('Mod_Video.stream_sources')
		));

		return $this->oTpl->fetch('Edit.html');
    }
}

?>